import asyncio

import config
import genius
import segmenter
import utils
from utils import os
import youtube


youtube_url = "https://www.youtube.com/playlist?list=PL6HdtK-ZQcnDWAMVIRXj-_bcBb0QxVQXf"
genius_url = "https://genius.com/albums/Eminem/The-eminem-show"



try:
	youtube_url
except NameError:
	youtube_url = None
try:
	genius_url
except NameError:
	genius_url = None

async def main():
	hash = utils.hash(youtube_url)
	path = os.mkdirs(os.path_join("temp", hash))

	tasks = []
	intervals = None
	youtube_filename = None
	if youtube_url:
		youtube_filename = hash + ".mp3"
		tasks, intervals = await youtube.download(youtube_url, path, youtube_filename)

	if genius_url is not None:
		task = asyncio.create_task(genius.album(genius_url, path))
		tasks.append(task)

	tasks_result = await asyncio.gather(*tasks)

	if intervals:
		await segmenter.slice(path, youtube_filename, intervals)

	if genius_url is None:
		return

	album = tasks_result[-1]

	playlist_folder = utils.format(config.playlist_folder_template, album)
	dist = os.mkdirs(os.path_join(config.music_folder, playlist_folder))

	cover = os.path_join(path, "cover.jpg")
	if not os.path_exists(cover):
		await utils.wget(album["cover_url"], cover)

	await os.copy_folder(path, dist, [youtube_filename])

	await utils.set_meta_to_albumfolder(album, dist)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
